﻿using System.Data.Common;
using System.Diagnostics;
using System.IO;
using System.Text;

namespace DotNet.Common.Extensions
{
    /// <summary>
    /// String扩展方法
    /// </summary>
    public static partial class StringExtensions
    {
        /// <summary>
        /// 指示指定的字符串是 null 还是 System.String.Empty 字符串。
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNullOrEmpty(this string s)
        {
            return string.IsNullOrEmpty(s);
        }

        public static bool IsNotNullOrEmpty(this string s)
        {
            return !s.IsNullOrEmpty();
        }

        /// <summary>
        ///  指示指定的字符串是 null、空还是仅由空白字符组成。
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNullOrWhiteSpace(this string s)
        {
            if (s.IsNullOrEmpty()) return true;
            return s.Trim().IsNullOrEmpty();
        }

        public static bool IsNotNullOrWhiteSpace(this string s)
        {
            return !s.IsNullOrWhiteSpace();
        }

        /// <summary>
        /// 将指定 System.String 中的格式项替换为指定数组中相应 System.Object 实例的值的文本等效项。
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static string FormatWith(this string format, params object[] args)
        {
            return string.Format(format, args);
        }

        /// <summary>
        /// 字符串转为Camel格式
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToCamel(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            return s[0].ToString().ToLower() + s.Substring(1);
        }

        /// <summary>
        /// 字符串转为Pascal格式
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static string ToPascal(this string s)
        {
            if (s.IsNullOrEmpty()) return s;
            return s[0].ToString().ToUpper() + s.Substring(1);
        }

        /// <summary>
        /// 执行SQL语句
        /// </summary>
        /// <param name="sql">SQL语句字符串</param>
        /// <param name="conn">数据库连接对象</param>
        /// <returns></returns>
        public static object ExecuteScalar(this string sql, DbConnection conn)
        {
            object result;
            using (DbCommand cmd = conn.CreateCommand())
            {
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.CommandType = System.Data.CommandType.Text;
                conn.Open();
                result = cmd.ExecuteScalar();
                conn.Close();
            }
            return result;
        }

        /// <summary>
        /// 执行DOS命令
        /// </summary>
        /// <param name="cmd">DOS命令字符串</param>
        /// <returns></returns>
        public static string ExecuteDOS(this string cmd)
        {
            Process process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardInput = true;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            process.StandardInput.WriteLine(cmd);
            process.StandardInput.WriteLine("exit");
            return process.StandardOutput.ReadToEnd();
        }

        /// <summary>
        ///打开一个文件，使用指定的编码读取文件的所有行，然后关闭该文件。
        /// </summary>
        /// <param name="path">要打开以进行读取的文件。</param>
        /// <param name="encoding">应用到文件内容的编码。</param>
        /// <returns>包含文件所有行的字符串。</returns>
        public static string ReadAllFileText(this string path, Encoding encoding = null)
        {
            return File.ReadAllText(path, encoding ?? Encoding.Default);
        }
    }
}
